import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/filter_utils.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_event_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_field_parameter_bean.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class EventController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? search;

  /// 筛选列表
  List<FilterBean> allFilterList = [];

  /// 筛选字段数据
  List<FilterFieldParameterBean> fieldParameterList = [];

  /// 筛选字段请求数据
  Map<String, dynamic> filterMap = {};

  /// 选择的开始时间
  String? startTime;

  /// 选择的结束时间
  String? endTime;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 添加筛选列表
    fieldParameterList = [
      FilterFieldParameterBean(
        screenName: '事故类型',
        parameterName: 'accidentType',
        dictionaryType: 'sglx_6Il0PlRq',
      ),
      FilterFieldParameterBean(
        screenName: '调度状态',
        parameterName: 'schedulingStatus',
      ),
      FilterFieldParameterBean(
        screenName: '事故发生时间',
        parameterName: '',
      ),
    ];
    // 获取字典筛选列表
    allFilterList = await FilterUtils.getDictFilterList(fieldParameterList: fieldParameterList);
    // 添加调度状态筛选条件
    final List<FilterEntitiesBean> stateList = [];
    // 添加状态筛选条件
    stateList.add(FilterEntitiesBean(name: '未调度', value: 0));
    stateList.add(FilterEntitiesBean(name: '已调度', value: 1));
    // 添加时间类型
    final List<FilterEntitiesBean> timeList = [];
    timeList.add(FilterEntitiesBean(name: '开始时间', value: startTime));
    timeList.add(FilterEntitiesBean(name: '结束时间', value: endTime));

    //筛选控制bean
    allFilterList.add(FilterBean(title: '调度状态', filterList: stateList));
    allFilterList.add(FilterBean(title: '事故发生时间', filterList: timeList));
    update();
  }

  Future<List<EmergencyEventListEntity>> getListData(int pageNum, int pageSize) async {
    filterMap.clear();
    final Map<String, dynamic> queryParameters = {
      'pageNum': pageNum,
      'pageSize': pageSize,
      'accidentName': search,
      'startTime': startTime,
      'endTime': endTime,
    };
    // 获取筛选处参数
    for (var item in fieldParameterList) {
      if (item.parameterValue != null) {
        filterMap[item.parameterName] = item.parameterValue;
      }
    }
    queryParameters.addAll(filterMap);
    // 获取列表接口
    final res = await HttpUtil()
        .generateGetRequest<List<EmergencyEventListEntity>>(
          Interface.getAccidentInfoPageList,
          queryParameters: queryParameters,
        )
        .execute();
    List<EmergencyEventListEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    allFilterList = value;
    // 参数清空
    for (var item in fieldParameterList) {
      item.parameterValue = null;
    }
    startTime = null;
    endTime = null;
    for (var i in value) {
      if (i.title == '事故发生时间') {
        for (var item in i.filterList) {
          switch (item.name) {
            case '开始时间':
              if (item.value != null && item.value != '') {
                startTime = item.value as String;
              }
              break;
            case '结束时间':
              if (item.value != null && item.value != '') {
                endTime = item.value as String;
              }
              break;
          }
        }
      }
      if (i.chosenValue.isEmpty) {
        continue;
      }
      // 获取标题相同的参数实体
      final result = fieldParameterList.firstWhere((data) => data.screenName == i.title);
      // 单选所以选中第一个就可以
      result.parameterValue = i.chosenValue[0].value;
    }
    update();
    refreshPage();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
